package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RoleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
    private RoleService roleService = BeanFactory.getBean("roleService",RoleService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            
            List<Role> list = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    private Result updateRoleAndModules(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数
            String roleId = request.getParameter("roleId");
            String[] moduleIds = BeanUtil.fillBeanFromJson(request, String[].class);
            //2 调用service层方法，查询所有数据
            roleService.updateRoleAndModules(roleId,moduleIds);
            //3 响应结果
            return new Result(true, MessageConstant.SETTING_ROLE_AUTHORIZE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SETTING_ROLE_AUTHORIZE_FAIL);
        }
    }
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            
            roleService.delete(ids);
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            
            roleService.update(role);
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            
            roleService.save(role);
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageStr)){
                currentPage=Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)){
                pageSize=Integer.parseInt(pageSizeStr);
            }
            
            PageInfo<Role> info =roleService.findByPage(currentPage,pageSize);
            return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMPANY_FAIL);
        }

    }

    
}
